﻿using AutoMapper;
using MediatR;
using Production_Set.API.MaterialManagement.Add;
using Return.Enum;
using SlaughterItem.Common.Infrastructures.Interfaces;
using THE_WIND_RISES.Common.Domans.Produce.DemandPlanning;
using THE_WIND_RISES.Common.Domans.Produce.Resources;
using THE_WIND_RISES.Common.Dtos;

namespace Production_Set.API.MaterialManagement.Update
{
    public class MaterialPlanningUpdateHandler : IRequestHandler<MaterialPlanningUpdateCommon, Result>
    {
        private readonly IBaseRepository<MaterialPlanning> MaterialPlanningRepository;
        public IMapper mapper { get; set; }
        public MaterialPlanningUpdateHandler(IBaseRepository<MaterialPlanning> materialPlanningRepository, IMapper mapper)
        {
            MaterialPlanningRepository = materialPlanningRepository;
            this.mapper = mapper;
        }

        public async Task<Result> Handle(MaterialPlanningUpdateCommon request, CancellationToken cancellationToken)
        {
            var results = new Result() { Code = (int)CommonErrorCodes.Delete };

            var AddData = mapper.Map<MaterialPlanning>(request);
            string sql = "UPDATE MaterialPlanning SET MaterialPlanningId=@MaterialPlanningId,  PlanningCode=@PlanningCode ,PlanningName=@PlanningName ,PlanningDesc=@PlanningDesc ,PlanningState=@PlanningState " +
                         ",PlanningPeole=@PlanningPeole ,DepartmentId=@DepartmentId,Handle=@Handle,HandleContext=@HandleContext,HandleTime=@HandleTime,Pid=@Pid,IsDelete=@IsDelete WHERE MaterialPlanningId =@MaterialPlanningId";
            var list = MaterialPlanningRepository.Update(sql, new
            {
                MaterialPlanningId = AddData.MaterialPlanningId,
                PlanningCode = AddData.PlanningCode,
                PlanningName = AddData.PlanningName,
                PlanningDesc = AddData.PlanningDesc,
                PlanningState = AddData.PlanningState,
                PlanningPeole = AddData.PlanningPeole,
                DepartmentId = AddData.DepartmentId,
                Handle = AddData.Handle,
                HandleContext=AddData.HandleContext,
                HandleTime=AddData.HandleTime,
                Pid = AddData.Pid,
                IsDelete = AddData.IsDelete
            });
            if (list != null)
            {
                results.Code = (int)CommonErrorCodes.Success;
                results.Msg = "修改成功";
            }

            return results;
        }
    }
}
